﻿// review form
FlyTalk.views.ComposeView = Ext.extend(Ext.form.FormPanel, {

	
    initComponent: function () {
		// back button on toolbar
		this.backButton = new Ext.Button({
			text: 'Back',
			ui: 'back',
			handler: this.backButtonTap,
			scope: this
		});
		
		this.postButton = new Ext.Button({
            text: 'Post',
            ui: 'action',
            handler: this.postButtonTap,
            scope: this
        });
		
		// top tool bar
		this.composeToolbar = new Ext.Toolbar({
			title: 'Review',
			
			items:[
				this.backButton,
				{ xtype: 'spacer' },
				this.postButton,
			]
			
		});

        this.dockedItems = [this.composeToolbar];

        FlyTalk.views.ComposeView.superclass.initComponent.call(this);
    },	

    backButtonTap: function () {
		this.reset();
//        Ext.dispatch({
//            controller: FlyTalk.controllers.ftController,
//            action: 'openHome'
//        });
		var next = FlyTalk.views.TalkView.tweetToolbar.title; // see which page talk is on right now
		Ext.dispatch({
            controller: FlyTalk.controllers.ftController,
            action: 'openNext',
            next: next,
        });
    },
	
	
	postButtonTap: function () {
		var user = Ext.getCmp('user').getValue();	
		var tp = Ext.getCmp('selectrestaurant').getValue();
		var recRest = Ext.getCmp('recRest').checked;
		var recFood = Ext.getCmp('recFood').checked;
		var eat = Ext.getCmp('eatfield').getValue();
		var cost = Ext.getCmp('costfield').getValue();
		var wait = Ext.getCmp('waitfield').getValue();
		var comment = Ext.getCmp('reviewComment').getValue();
		
		this.reset();
		if(recRest == true){
			recRest = 1;
		}else{
			recRest = 2;
		}
		
		if(recFood == true){
			recFood = 1;
		}else{
			recFood = 2;
		}
		
		Ext.dispatch({
            controller: FlyTalk.controllers.dbController,
            action: 'saveReview',
            uid: user,
            recRest: recRest,
            recFood: recFood,
            eat: eat,
            cost: cost,
            wait: wait,
            comment: comment,
            tp: tp,
		});
    },
	
	scroll: true,
	
		xtype: 'text',
        label: 'Little Haven',
	
	items: [
	{ // select a user (researcher mode only!) - THIS FIELD HAS TO BE THE FIRST. DO NOT CHANGE!
	    xtype: 'selectfield',
	    name: 'user',
		id: 'user',
	    label: 'select user',
	    labelWidth: 300,
		labelAlign: 'top',
		options: [
	   		{text: 'HappyTraveler',  value: '3'},
	   		{text: 'Jessica', value: '4'},
	   		{text: 'TAP Airline',  value: '1'},
	   		{text: 'Airport',  value: '2'},
		]
	},{html: ''},
	{ // select a tag
        xtype: 'selectfield',
        name: 'restaurant',
		id: 'selectrestaurant',
        label: 'Which restaurant did you go?',
        labelWidth: 300,
		labelAlign: 'top',
		options: [
       		{text: 'Little Haven Restaurant',  value: 'Little Haven Restaurant'},
       		{text: 'Delta Cafe', value: 'Delta Cafe'},
       		{text: 'Dulce Cafe',  value: 'Dulce Cafe'},
    	]
	},
	{ // recommend restaurant
		xtype: 'fieldset',
		id: 'recRest',
		title: 'Would you recommend this restaurant?',
			defaults: {
				xtype: 'radiofield',
				labelwidth: '35%',
			},
			items: [{
				name: 'recommend',
				value: 'yes',
				label: 'Yes',
				id: 'recRestYes',
				listeners:{
					check: function(dv, tweet, index, evt){
						Ext.getCmp('recRest').checked = true;
					},
				},
			},{
				name: 'recommend',
				value: 'no',
				label: 'No',
				listeners:{
					check: function(dv, tweet, index, evt){
						Ext.getCmp('recRest').checked = false;
					},
				},
			}]
	},
	{ // what did you eat?
        xtype: 'textfield',
        name: 'eat',
		id: 'eatfield',
        label: 'What did you eat?',
        labelWidth: 300,
		labelAlign: 'top',
        required: true
   	},{html: ''},
   	{ // how much did it cost?
		xtype: 'textfield',
        name: 'cost',
		id: 'costfield',
        label: 'How much did it cost? (euro)',
        labelWidth: 300,
		labelAlign: 'top',
		required: true,
	},{html: ''},
	{ // how long did you wait?
        xtype: 'textfield',
        name: 'wait',
		id: 'waitfield',
		labelWidth: 300,
		labelAlign: 'top',
        label: 'How long did you wait? (min)',
        required: true,
	//	labelwidth: '50%'
   	},{ // would you recommend what you ate?
		xtype: 'fieldset',
		id: 'recFood',
		title: 'Would you recommend what you ate?',
			defaults: {
				xtype: 'radiofield',
				labelwidth: '35%',
			},
			items: [{
				name: 'recommend',
				value: 'yes',
				label: 'Yes',
				listeners:{
					check: function(dv, tweet, index, evt){
						Ext.getCmp('recFood').checked = true;
					},
				},
			},{
				name: 'recommend',
				value: 'no',
				label: 'No',
				listeners:{
					check: function(dv, tweet, index, evt){
						Ext.getCmp('recFood').checked = false;
					},
				},
			}]
	},{ // comment
        xtype: 'textfield',
        name: 'comment',
        hidden: true,
		id: 'reviewComment',
		labelWidth: 300,
		labelAlign: 'top',
        label: 'Comment',
        required: true,
   	},
	{ // add comment button
		xtype:'button',
		text: 'Add Comment',
		id: 'addComment',
		style: 'margin: .5em',
		handler: function() {
			Ext.getCmp('reviewComment').show();
			Ext.getCmp('addComment').hide();
		}
	},
	/*{
		xtype:'button',
		text: 'Submit',
		style: 'margin: 1em .5em .5em .5em',
		handler: function() {
		}
	}*/
	]
});

